<?php

namespace App\Admin\Forms;

use App\Models\ProjectExportRecordsDetail;
use Dcat\Admin\Admin;
use Dcat\Admin\Contracts\LazyRenderable;
use Dcat\Admin\Traits\LazyWidget;
use Dcat\Admin\Widgets\Form;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class UploadReceiptBatchToolsForm extends Form implements LazyRenderable
{
    use LazyWidget;

    public $elementId = 'receipt-form';

    /**
     * Handle the form request.
     *
     * @param array $input
     *
     * @return mixed
     */
    public function handle(array $input)
    {

        // 获取选中的导出记录
        $recordDetailList = ProjectExportRecordsDetail::whereIn('id', explode(',', $input['id']))->get();

        try {

            foreach ($recordDetailList as $key => $val) {
                $lastLog = json_decode($val['audit_log'], true);
                \App\Models\ProjectAgreement::where('id', $val['project_agreement_id'])
                    ->update([
                        'receipt_form' => $input['receipt_form'],
                    ]);


                \App\Models\ProjectExportRecordsDetail::where('id', $val['id'])
                    ->update([
                        'receipt_form' => $input['receipt_form'],
                    ]);
            }

            return $this
                ->response()
                ->success('操作成功')
                ->refresh();
        } catch (\Exception $e) {
            Log::info('----------catch----------', ['exception' => json_encode($e)]);
            return $this
                ->response()
                ->error('操作失败')
                ->refresh();
        }
    }


    /**
     * Build a form here.
     */
    public function form()
    {
        $this->multipleImage('receipt_form', '上传回执单')
            ->accept('jpg,png,gif,jpeg')
            ->help('只允许上传jpg、png、gif、jpeg格式的图片文件')
            ->saveFullUrl()
            ->autoUpload()
            ->rules('required_if:status,1', ['required_if' => '请填写完整该项'])
            ->saving(function ($value) {
                return implode(',', httpToHttps($value));
            });
        // 设置隐藏表单，传递用户id
        $this->hidden('id')->attribute('id', 'reset-password-id');
    }

    /**
     * The data of the form.
     *
     * @return array
     */
    public function
    default()
    {

        return [];
    }
}
